package gang.com.mobelhelper.common.rx.subscriber;

import android.app.ProgressDialog;
import android.content.Context;

/**
 * Created by xiaogangzai on 2017/11/26.
 */

public abstract class ProgressDialogSubscriber<T> extends ErrorHandlerSubscriber<T> {
    private ProgressDialog mProgressDialog = null;
    private Context mContext;

    public ProgressDialogSubscriber(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (isShowProgressDialog()) {
            showDialog();

        }
    }

    @Override
    public void onCompleted() {
        if (isShowProgressDialog()) {
            dismissDialog();
        }
    }


    public void initDialog() {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(mContext);
        }
    }


    @Override
    public void onError(Throwable e) {
        //先执行父类的方法，然后执行下面的显示方法
        super.onError(e);
        if (isShowProgressDialog()) {
            dismissDialog();
        }
    }

    public void showDialog() {
        initDialog();
        mProgressDialog.show();
    }

    public void dismissDialog() {
        mProgressDialog.dismiss();
    }

    //判断是否显示dialog
    public boolean isShowProgressDialog() {
        return true;
    }
}
